package jp.ac.nitech.matlab.najm.net;


/**
 * <[  بسم الله الرحمان الرحيم  ]
 * @author k-hon
 *
 */

public class Packet {

	public static final int CTRL = 16; //CMDCODE000000000
	public static final int DATA4 = 4096; //size of data packets
	public static final int DATA8 = 8192; //8192 bytes
	public static final int DATA16 = 16384; //16384
	public static final int DATA32 = 32768; //32768
	public static final int DATA64 = 65536; //65536
	
	//private int size; // = 4096;
	public byte[] buffer;
	
	private int type = -1;  //type == size
	
	public boolean isData(){
		return (type!=CTRL)? true:false;
	}
	
	public Packet(int type){
		this.type = type;
		buffer = new byte[type];  //type == size
	}
	
	public byte[] getBuffer(){
		return buffer;
	}
	
	/*        CONTROL     */
	public String getControl(){
		return new String(buffer);
	}
	public String getCtrlTarget(){
		return getControl().substring(7);
	}
	public String getCtrlCmd(){
		return getControl().substring(0, 3);
	}
	//sets the packet, assumes cmd(3), code(4), target(9)
	public void setControl(String cmd, String code, String target){
		buffer = (cmd+code+target).getBytes();
	}

	class DataPacket extends Packet{
		public DataPacket(){
			super(Packet.DATA4);
		}
	}
}
